/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lmx.repoDomain.repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lmx.common.constants.CommonConstants;
import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.CustomerService;
import com.lmx.repoDomain.exception.BusinessException;
import com.lmx.repoDomain.tmodel.EvaluateModel;

import io.katharsis.queryParams.QueryParams;
import io.katharsis.repository.annotations.JsonApiFindAll;
import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.repository.annotations.JsonApiSave;

@JsonApiResourceRepository(EvaluateModel.class)
@Component
public class EvaluateModelRepository extends BaseRepository<EvaluateModel>{
	
	@Autowired
	private CustomerService customerService;

    @PostConstruct
    public void init() {
        dslJpaService.setEntityClass(EvaluateModel.class);
    }

	@Override
	@JsonApiSave
	public EvaluateModel save(EvaluateModel model) {
		if (model.getId() == null) {
			EvaluateModel temp = customerService.getEvaluateModelByTitle(model.getTitle());
			if (temp == null) {
				model = super.save(model);
			} else {
				model.setErrcode(4);// 名字重复
				throw new BusinessException(null, CommonConstants.ERROR_CODE.NAME_ERROR.label(), null);
			}
		} else {
			if (model.getDelStatus() == 1) {// 删除
				dslJpaService.update(model);
				// nursingService.deleteNursrGroup(level.getId());
			} else {
				int updateFlag = 1;
				EvaluateModel temp = customerService.getEvaluateModelByTitleFilterId(model.getTitle(), model.getId());
				EvaluateModel old = super.findOne(model.getId(), null);
				if (temp == null) {

				} else {
					updateFlag = 0;
				}
				if (updateFlag == 1) {
					if (old!=null && old.getUseFlag() != model.getUseFlag()) {
						dslJpaService.update(model);
					}else{
						model = super.save(model);
					}
				} else {
					model.setErrcode(4);// 名字重复
					throw new BusinessException(null, CommonConstants.ERROR_CODE.NAME_ERROR.label(), null);
				}
			}
		}
		return model;
	}

	@Override
	@JsonApiFindAll
	public Iterable<EvaluateModel> findAll(final QueryParams requestParams) {
		Map<String, String> keyMap = CommonUtil.getKeyMapByFilter(requestParams);
		if (keyMap.containsKey("queryTenant")) {
			List dataList = new ArrayList<>();
			dataList = customerService.getTenantEvaluateModel(keyMap.get("titleLike"),keyMap.get("typecode"));
			commonService.setPageInfo(dataList, requestParams);
			dataList = commonService.getPageData(dataList, requestParams);
			return dataList;
		}else{
			return super.findAll(requestParams);
		}

	}
	
}
 